<?php
class Accept_help extends CI_Controller{

	public function __construct()
	{
		parent::__construct();
		//登录校验
		$this->common->check_login();
		//通过user_model的get_user_info_id方法获取个人信息
		$this->user_info = $this->user->get_user_info_id($_SESSION['user']['id']);

		$this->load->model('order_model');
	}

	/**
	 * 接受帮助
	 */
	public function index()
	{
		$team_arr = $this->common->team_lv_rate();
		$team_lv = $team_arr[$this->user_info['team_lv']];
		$this->load->helper('form');

		$data['left'] = $this->load->view('left', '', TRUE);

		$data['menu'] = $this->load->view('menu', '', TRUE);

		$data['user'] = $this->user_info;
		
		
		$data['maxmoney'] = $team_lv['max'];

		$data['order_type'] = array('静态钱包','推广钱包');

		$data['dakuan_type'] = array('对方未打款','已经打款','拒绝打款');
		//echo $this->user_info['id'];
		$data['log_list'] = $this->order_model->accept_order_log_list_uid($this->user_info['id']);
		$this->load->view('accept_help', $data, FALSE);
	}

	/**
	 * 信息提交
	 */
	public function info_post()
	{
		if(empty($_POST) OR empty($_POST['money']))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}
		//获取本人最新提供帮助信息
		$offer_info = $this->order_model->get_new_offer_order($this->user_info['id']);

		$ol_info = $this->order_model->order_log_list(0,$offer_info['id']);
		//var_dump($ol_info);exit;
		//必须有未匹配的单子在可以接受帮助
		if( empty($offer_info) || !empty($ol_info))
		{
			exit($this->common->alert_msg_goback("请先提供公益帮助再接受公益帮助！"));
		}

		

		//每天静态和推广可以获得一次帮助
		$num = $this->order_model->accept_order_day($this->user_info['id'],$_POST['order_type']);
		// echo $num;
		// exit;
		// $num = $this->order_model->get_order_shoukuan_check($this->user_info['id'],$_POST['order_type']);

		// $accept = $this->order_model->accept_order_row_check($this->user_info['id'],$_POST['order_type']);

		if($num > 0)
		{
			exit($this->common->alert_msg_goback("静态金额或动态金额每天只能匹配一次!"));
			//exit($this->common->alert_msg_goback("还有尚未匹配的公益项目!"));
		}

		if($_POST['order_type'] == 0)
		{
			$s_money = 2000;
			//1000倍数
			$mod_num = 100;

			$max_money = $this->user_info['jt_money'];
		}
		else
		{
			$s_money = 1000;
			//500倍数
			$mod_num = 500;

			$max_money = $this->user_info['tg_money'];
		}

		if( $_POST['money'] > $max_money)
		{
			exit($this->common->alert_msg_goback('余额不足'));
		}

		if( $_POST['money'] < $s_money)
		{
			exit($this->common->alert_msg_goback('最小提现金额'.$s_money.'元'));
		}

		if(!is_int( $_POST['money'] / $mod_num ))
		{
			exit($this->common->alert_msg_goback('请输入正确金额'));
		}

		//新增订单
		$bianhao = $this->common->getnumChar ( 7 );
		//新增获取订单
		$result = $this->order_model->accept_order_insert($this->user_info['id'],$_POST['order_type'],$_POST['money'],$bianhao);

		if($result)
		{
			//更新钱包金额
			$this->user->update_minus_user_money($_POST['order_type'],$_POST['money'],$this->user_info['id']);

			exit($this->common->alert_msg_url('提交成功',site_url('accept_help')));
		}
		else
		{
			exit($this->common->alert_msg_goback('提交失败'));
		}

	}


	/**
	 * 更新收款状态
	 */
	public function change_shoukuan_type($type,$id)
	{
		if(empty($type) || empty($id))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}

		$log = $this->order_model->get_order_log_id($id);

		//24小时
		// $sy_time = 24 - floor((time() - $log['create_time'])/3600);

		// if($sy_time <= 0)
		// {
		// 	exit($this->common->alert_msg_goback('已过期'));
		// }
		//更新打款状态
		$result = $this->order_model->update_order_log_shoukuan($type,$id);
		//var_dump($result);exit;
		
	   	if($result)
	   	{
			if($type == 1)
			{
				//5小时之内有积分
				$five_hours = 18000 - ($log['dakuan_time'] - $log['create_time']);

				$offer = $this->order_model->offer_order_by_id($log['offer_order_id']);

				$users = $this->user->get_user_info_id($offer['user_id']);

				if($five_hours > 0)
				{
					$score = $users['score'] + ($log['money'] / 100);
					//更新积分
					$this->user->update_score($score,$users['id']);

				}
				//团队
				$this->five_generation($users['team_lv'],$log['offer_order_id'],'one',urldecode($users['up_account']),$offer['money']);
				
				//全部匹配都打款
				$this->order_model->offer_order_pay_on($log['offer_order_id']);

				$this->do_status($log['offer_order_id'],$log['accept_order_id']);
				
				// 短信
				$mobile = $users['mobile'];
				$content = '会员已完成对您的收款确认，请登陆平台进行查看。';
				//$this->common->send_sms($mobile,$content);
				
				exit($this->common->alert_msg_url('确认收款',site_url('accept_help')));
			}
			else
			{
				exit($this->common->alert_msg_url('未收到收款',site_url('accept_help')));
			}
	   	}else
	   	{
	   		exit($this->common->alert_msg_url('更新失败',site_url('accept_help')));
	   	}
	}

	private function do_status($offer_order_id,$accept_order_id) {
		
		$result = $this->order_model->order_by_offer_id($offer_order_id);
		if (!$result) {
			$this->order_model->update_offer_status($offer_order_id);
		}
		
		$result = $this->order_model->order_by_accept_id($accept_order_id);
		if (!$result) {
			$this->order_model->update_accept_status($accept_order_id);
		}

	}
	
	/**
	*d_lv 下级团队等级
	 * 每次确认向上追溯5代按不同等级分红，每天有上限
	 *订单日志ID
	 *$dai 第几代 one、two、three、four、five
	 *$up_account收款方的上级
	 */
	private function five_generation($d_lv,$offer_order_id,$dai,$up_account,$money)
	{
		
		$info = $this->user->get_uesr_info($up_account);
		//echo $d_lv.'-----'.$info['team_lv'];
		//echo '<br>';
		// echo $dai;
		// echo '<br>';
		//var_dump($info);
		//存在上代
		if(!empty($info))
		{
			//echo 111;
			$team_arr = $this->common->team_lv_rate();

			$team_lv = $team_arr[$info['team_lv']];
			//var_dump($team_lv);
			$rate = $team_lv[$dai];
			//var_dump($rate);
			//此代分红利率不为0
			if(!empty($rate))
			{
				//此笔交易未分红
				$log = $this->order_model->get_team_log($info['id'],$offer_order_id);
				//var_dump($log);
				if(empty($log))
				{
					//提供帮助订单是否已打款money
					$dk_money = $this->order_model->get_offer_order_dakuan_money($offer_order_id);
					//var_dump($dk_money);
					if(empty($dk_money) || $dk_money['sum_money'] != $dk_money['money'] )
					{
						return TRUE;
					}

					//等级比上级低或者是直推第一代，都有分红
					if($d_lv <= $info['team_lv'] || $dai == 'one'){
						//用上级的等级对比上上级
						$d_lv = $info['team_lv'];
						//echo $d_lv.'--';
						$max = $team_lv['max'];
						//获取当日金额
						$sum_money = $this->order_model->get_team_log_day_money($info['id']);
						//烧伤判断 获取上代的提供帮助订单金额
						$up_order = $this->order_model->get_new_offer_order($info['id']);
						

						//不存在帮助订单
						if(empty($up_order))
						{
							//$money = 0;
							$fh_money = 0;
						}
						elseif($up_order['money'] < $money)
						{
							//$money = $up_order['money'];
							//分红money
							$fh_money = $up_order['money'] * ($rate/100) ;
						} else {
							$fh_money = $money * ($rate/100) ;
						}
						
						//先加再判断是否超上限
						$sum_money += $fh_money;
						//echo $sum_money;
						//未超过当日分红上限
						//if($max >= $sum_money)
						//{
							//$data['ol_id']       = $ol_id;
							$data['user_id']     = $info['id'];
							$data['dai']         = $dai;
							$data['money']       = $fh_money;
							$data['create_time'] = time();
							//参与分红用户的上代账号
							$data['up_account']  = $info['up_account'];

							$data['offer_order_id'] = $offer_order_id;
							//var_dump($data);
						    $this->db->insert('pc_ddth_team_log', $data);

						    $this->user->update_user_tgmoney($fh_money,$info['id']);
						//}
					}
						
					//等级比上级高，一直用当前等级和上几代做对比，等级比上级低
				}
			}
			
			if($dai == "one")
			{
				$this->five_generation($d_lv,$offer_order_id,'two',$info['up_account'],$money);
			}
			elseif($dai == "two")
			{
				$this->five_generation($d_lv,$offer_order_id,'three',$info['up_account'],$money);
			}
			elseif($dai == 'three')
			{
				$this->five_generation($d_lv,$offer_order_id,'four',$info['up_account'],$money);
			}
			elseif($dai == 'four')
			{
				$this->five_generation($d_lv,$offer_order_id,'five',$info['up_account'],$money);
			}
			else{
				return TRUE;
			}

		}else
		{
			return TRUE;
		}


	}
}
